<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Family Members - 单词学习游戏</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial Rounded MT Bold', 'Arial', sans-serif;
        }
        
        body {
            background-color: #fff;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px;
        }
        
        header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        h1 {
            color: #2c3e50;
            font-size: 2.8rem;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
        }
        
        .instructions {
            color: #7f8c8d;
            font-size: 1.2rem;
            max-width: 600px;
            text-align: center;
            margin-bottom: 20px;
        }
        
        .game-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 100%;
            max-width: 900px;
        }
        
        .house-container {
            width: 450px;
            height: 400px;
            margin-bottom: 40px;
            position: relative;
        }
        
        .house {
            width: 100%;
            height: 100%;
            background-color: #f9d56e;
            border: 6px solid #e8505b;
            border-bottom: none;
            border-radius: 50% 50% 0 0;
            position: relative;
            overflow: hidden;
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        
        .roof {
            position: absolute;
            top: -40px;
            left: -20px;
            width: 110%;
            height: 100px;
            background-color: #e8505b;
            transform: rotate(-5deg);
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        
        .door {
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 80px;
            height: 120px;
            background-color: #8b4513;
            border: 4px solid #5d4037;
            border-bottom: none;
            border-radius: 10px 10px 0 0;
        }
        
        .window {
            position: absolute;
            width: 70px;
            height: 70px;
            background-color: #a7e9f2;
            border: 4px solid #5d4037;
            border-radius: 10px;
        }
        
        .window::after {
            content: '';
            position: absolute;
            width: 100%;
            height: 4px;
            background-color: #5d4037;
            top: 50%;
            transform: translateY(-50%);
        }
        
        .window::before {
            content: '';
            position: absolute;
            height: 100%;
            width: 4px;
            background-color: #5d4037;
            left: 50%;
            transform: translateX(-50%);
        }
        
        .window-left {
            top: 90px;
            left: 70px;
        }
        
        .window-right {
            top: 90px;
            right: 70px;
        }
        
        .words-container {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 15px;
            margin-top: 20px;
            padding: 20px;
            background-color: #f5f5f5;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            width: 100%;
        }
        
        .word-card {
            padding: 15px 25px;
            background: linear-gradient(145deg, #3498db, #2980b9);
            color: white;
            border-radius: 10px;
            font-size: 1.2rem;
            cursor: pointer;
            user-select: none;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            transition: all 0.2s ease;
        }
        
        .word-card:nth-child(2n) {
            background: linear-gradient(145deg, #e74c3c, #c0392b);
        }
        
        .word-card:nth-child(3n) {
            background: linear-gradient(145deg, #2ecc71, #27ae60);
        }
        
        .word-card:nth-child(4n) {
            background: linear-gradient(145deg, #f39c12, #d35400);
        }
        
        .word-card:nth-child(5n) {
            background: linear-gradient(145deg, #9b59b6, #8e44ad);
        }
        
        .word-card:nth-child(6n) {
            background: linear-gradient(145deg, #1abc9c, #16a085);
        }
        
        .word-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 6px 12px rgba(0,0,0,0.15);
        }
        
        .word-card:active {
            transform: scale(0.95);
        }
        
        .dropped-word {
            position: absolute;
            padding: 10px 18px;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 8px;
            font-size: 1.1rem;
            box-shadow: 0 3px 10px rgba(0,0,0,0.15);
            pointer-events: none;
            z-index: 10;
            text-align: center;
            min-width: 120px;
            border: 2px solid #3498db;
            color: #2c3e50;
            font-weight: bold;
        }
        
        .success-message {
            margin-top: 20px;
            padding: 15px;
            background-color: #2ecc71;
            color: white;
            border-radius: 10px;
            text-align: center;
            display: none;
            animation: fadeIn 0.5s ease;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }
        
        .reset-btn {
            margin-top: 20px;
            padding: 12px 25px;
            background-color: #e74c3c;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 1rem;
            cursor: pointer;
            transition: background-color 0.3s;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        
        .reset-btn:hover {
            background-color: #c0392b;
        }
        
        .counter {
            margin-top: 15px;
            font-size: 1.1rem;
            color: #7f8c8d;
        }
        
        .house-interior {
            position: absolute;
            top: 50px;
            left: 30px;
            right: 30px;
            bottom: 30px;
            pointer-events: none;
        }
    </style>
</head>
<body>
    <header>
        <h1>My Family Members</h1>
        <p class="instructions">点击家庭成员单词，它们会自动出现在房子中！单词不会互相遮挡。</p>
    </header>
    
    <div class="game-container">
        <div class="house-container">
            <div class="house">
                <div class="roof"></div>
                <div class="door"></div>
                <div class="window window-left"></div>
                <div class="window window-right"></div>
                <div class="house-interior" id="houseInterior">
                    <!-- 放置单词的区域 -->
                </div>
            </div>
        </div>
        
        <div class="words-container">
            <div class="word-card" data-word="mother">mother</div>
            <div class="word-card" data-word="father">father</div>
            <div class="word-card" data-word="grandmother">grandmother</div>
            <div class="word-card" data-word="grandfather">grandfather</div>
            <div class="word-card" data-word="sister">sister</div>
            <div class="word-card" data-word="brother">brother</div>
        </div>
        
        <div class="success-message" id="successMessage">太好了！单词已添加到房子中！</div>
        
        <div class="counter" id="counter">已添加单词: 0</div>
        
        <button class="reset-btn" id="resetBtn">重置游戏</button>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const wordCards = document.querySelectorAll('.word-card');
            const houseInterior = document.getElementById('houseInterior');
            const successMessage = document.getElementById('successMessage');
            const counter = document.getElementById('counter');
            const resetBtn = document.getElementById('resetBtn');
            
            let wordCount = 0;
            const placedWords = [];
            const gridSize = 80; // 网格大小，用于防止重叠
            
            // 获取随机位置，确保不与其他单词重叠
            function getRandomPosition(wordWidth, wordHeight) {
                const maxX = houseInterior.offsetWidth - wordWidth;
                const maxY = houseInterior.offsetHeight - wordHeight;
                
                let x, y;
                let attempts = 0;
                let validPosition = false;
                
                // 尝试找到不重叠的位置
                while (!validPosition && attempts < 100) {
                    x = Math.floor(Math.random() * maxX);
                    y = Math.floor(Math.random() * maxY);
                    
                    validPosition = true;
                    
                    // 检查是否与已放置的单词重叠
                    for (const placedWord of placedWords) {
                        if (x < placedWord.x + placedWord.width + 10 && 
                            x + wordWidth + 10 > placedWord.x &&
                            y < placedWord.y + placedWord.height + 10 &&
                            y + wordHeight + 10 > placedWord.y) {
                            validPosition = false;
                            break;
                        }
                    }
                    
                    attempts++;
                }
                
                return { x, y };
            }
            
            // 为每个单词卡片添加点击事件
            wordCards.forEach(card => {
                card.addEventListener('click', function() {
                    const word = this.dataset.word;
                    
                    // 创建放置的单词元素
                    const droppedWord = document.createElement('div');
                    droppedWord.classList.add('dropped-word');
                    droppedWord.textContent = word;
                    droppedWord.dataset.word = word;
                    
                    // 添加到房子中先获取尺寸
                    houseInterior.appendChild(droppedWord);
                    
                    const wordWidth = droppedWord.offsetWidth;
                    const wordHeight = droppedWord.offsetHeight;
                    
                    // 获取一个不重叠的位置
                    const position = getRandomPosition(wordWidth, wordHeight);
                    
                    // 设置位置
                    droppedWord.style.left = `${position.x}px`;
                    droppedWord.style.top = `${position.y}px`;
                    
                    // 记录已放置的单词
                    placedWords.push({
                        x: position.x,
                        y: position.y,
                        width: wordWidth,
                        height: wordHeight,
                        element: droppedWord
                    });
                    
                    // 显示成功消息
                    successMessage.style.display = 'block';
                    setTimeout(() => {
                        successMessage.style.display = 'none';
                    }, 1500);
                    
                    // 更新计数器
                    wordCount++;
                    counter.textContent = `已添加单词: ${wordCount}`;
                });
            });
            
            // 重置游戏
            resetBtn.addEventListener('click', function() {
                // 移除所有已放置的单词
                while (houseInterior.firstChild) {
                    houseInterior.removeChild(houseInterior.firstChild);
                }
                
                // 清空记录
                placedWords.length = 0;
                wordCount = 0;
                counter.textContent = `已添加单词: ${wordCount}`;
            });
        });
    </script>
</body>
</html>
